###### Figure A11 Script #####
rm(list=ls());gc();gc();gc();gc();gc();gc();gc();gc()


library(PanelMatch) #using PanelMatch 3.0.0
library(dplyr)


here::i_am("Scripts/FigA11Script.R")

library(here)

kl_e2<-readRDS(here("Data", "analysis_data2.rds"))

kl_e2$yearsess2<-kl_e2$yearsess
kl_e2$yearsess2[which(kl_e2$year==1998&kl_e2$sen==0)]<-"1999"
kl_e2$yearsess2[which(kl_e2$year==2000&kl_e2$sen==0)]<-"2001"
kl_e2$yearsess2[which(kl_e2$year==2002&kl_e2$sen==0)]<-"2003"
kl_e2$yearsess2[which(kl_e2$year==2004&kl_e2$sen==0)]<-"2005"
kl_e2$yearsess2[which(kl_e2$year==2006&kl_e2$sen==0)]<-"2007"
kl_e2$yearsess2[which(kl_e2$year==2008&kl_e2$sen==0)]<-"2009"
kl_e2$yearsess2[which(kl_e2$year==2010&kl_e2$sen==0)]<-"2011"
kl_e2$yearsess2[which(kl_e2$year==2012&kl_e2$sen==0)]<-"2013"
kl_e2$yearsess2[which(kl_e2$year==2014&kl_e2$sen==0)]<-"2015"
kl_e2$yearsess2[which(kl_e2$year==2016&kl_e2$sen==0)]<-"2017"
kl_e2$yearsess2<-as.numeric(kl_e2$yearsess2)

kl_e3<-kl_e2[which(kl_e2$yearsess2<=2017),]

kl_e3$time<-NA;
kl_e3$time[which(kl_e3$yearsess2==1999)]<-1
kl_e3$time[which(kl_e3$yearsess2==2001)]<-2
kl_e3$time[which(kl_e3$yearsess2==2003)]<-3
kl_e3$time[which(kl_e3$yearsess2==2005)]<-4
kl_e3$time[which(kl_e3$yearsess2==2007)]<-5
kl_e3$time[which(kl_e3$yearsess2==2009)]<-6
kl_e3$time[which(kl_e3$yearsess2==2011)]<-7
kl_e3$time[which(kl_e3$yearsess2==2013)]<-8
kl_e3$time[which(kl_e3$yearsess2==2015)]<-9
kl_e3$time[which(kl_e3$yearsess2==2017)]<-10
kl_e3$time<-as.integer(kl_e3$time)

cleaned_candid_noskip<-data.frame(cleaned_candid=unique(kl_e3$cleaned_candid),cleaned_candid_noskip=seq.int(1,length(unique(kl_e3$cleaned_candid)),1),stringsAsFactors = F)
kl_e3<-merge(kl_e3,cleaned_candid_noskip,by=c("cleaned_candid"),all.x=T)
kl_e3<-kl_e3[order(kl_e3$cleaned_candidyear),]


kl_e3$party2<-as.numeric(grepl("democrat",kl_e3$party)==T) #n.b. sole independent caucusing with reps (at least second time, first to be confirmed)
kl_e3$sab2<-as.factor(kl_e3$sab)
kl_e3$year3<-as.factor(kl_e3$year2)

kl_e3$ky<-as.numeric(kl_e3$sab=="KY")
kl_e3$mi<-as.numeric(kl_e3$sab=="MI")
kl_e3$oh<-as.numeric(kl_e3$sab=="OH")
kl_e3$wv<-as.numeric(kl_e3$sab=="WV")

kl_e3$legislative_seniority<- kl_e3$incumbent_legislature
kl_e3$yeardif<-kl_e3$yearsess2-kl_e3$year
kl_e3$nonelses<-0
kl_e3$nonelses[which(kl_e3$yeardif==3)]<-1
kl_e3$chair_no_other2<-as.numeric(kl_e3$chair_no_other2)
kl_e3$chairs_other<-as.numeric(kl_e3$chairs_other)

kl_e3$sen<-as.numeric(kl_e3$sen)
kl_e3$Leader<-as.numeric(kl_e3$leader)
kl_e3$IncMaj<-as.numeric(kl_e3$incmaj)

kl_e3$legislative_seniority<-as.numeric(kl_e3$legislative_seniority)

kl_e3$exc0<-as.numeric(kl_e3$sen==1 & kl_e3$yearsess2 ==2019 & kl_e3$sab=="WV" | kl_e3$sen==1 & kl_e3$yearsess2 ==2019 & kl_e3$sab=="OH" | kl_e3$sen==1 & kl_e3$yearsess2 ==2019 & kl_e3$sab=="KY" | kl_e3$cleaned_candid==262471)

panel.data1<-PanelData(panel.data=subset(kl_e3,limit1!=1&exc0==0), time.id = "time", unit.id = "cleaned_candid_noskip", treatment = "Leader",outcome="logfriamount")
DiD.matches_leader <- PanelMatch(panel.data=panel.data1,lag = 1, refinement.method = "CBPS.weight", 
                                 match.missing = T, 
                                 covs.formula =
                                   ~ I(lag(chair_no_other2,1))+I(lag(chairs_other,1)) +I(lag(nonelses,1))+I(lag(party2,1))+
                                   I(lag(IncMaj,1)) +  I(lag(legislative_seniority,1))  + I(lag(sen,1))+
                                   I(lag(logfriamount,1))
                                 + I(lag(mi,1))+ I(lag(oh,1))+I(lag(wv,1))+ I(lag(ky,1)),
                                 size.match = 20,
                                 qoi = "att",verbose=T,
                                 use.diagonal.variance.matrix = T,
                                 lead = 0, forbid.treatment.reversal = F)

DiD.results_leader <- PanelEstimate(confidence.level=c(0.9),sets = DiD.matches_leader, panel.data = panel.data1, df.adjustment = T,number.iterations = 20000)
summary(DiD.results_leader)
pdf(file=here("Results", "FigA11.pdf"),width = 9,height = 9)
par(mfcol=c(2,2))
plot(DiD.results_leader,main="(1) Leader Treatment",ylab="Estimated Average Effect of Treatment on Treated")

cb1<-get_covariate_balance(DiD.matches_leader, panel.data=panel.data1, covariates=c("IncMaj","party2","sen","chairs_other",
                                                                      "nonelses","legislative_seniority","chair_no_other2"))

plot(cb1[[1]]$att[,1],type="l",ylim=c(-2,2),ylab="SD",xaxt="n",xlab="Time",main="(2) Leader Treatment - Covariate Balance",)
lines(cb1[[1]]$att[,2],col="red")
lines(cb1[[1]]$att[,3],col="blue")
lines(cb1[[1]]$att[,4],col="yellow")
lines(cb1[[1]]$att[,5],col="green")
axis(side=1,at=c(1,2),c("t-1","t-0"))
abline(h=0,lty=2)
legend("topleft",col=c("black","red","blue","yellow","green"),lty=1,cex=0.9,legend=c("Majority Party","Party","Senate","Major Chair","Non-Election Cohort","Other Chair"))


panel.data2<-PanelData(panel.data=subset(kl_e3,limit1!=1&exc0==0), time.id = "time", unit.id = "cleaned_candid_noskip", treatment = "chair_no_other2",outcome="logfriamount")
DiD.matches_chair <- PanelMatch(lag = 1, panel.data=panel.data2, refinement.method = "CBPS.weight", 
                                 match.missing = T, verbose=T,
                                covs.formula =
                                  ~ I(lag(Leader,1)) +I(lag(nonelses,1))+I(lag(party2,1))+
                                  I(lag(IncMaj,1)) +  I(lag(legislative_seniority,1))  + I(lag(sen,1))+
                                  I(lag(logfriamount,1))+ I(lag(chairs_other,1))
                                + I(lag(mi,1))+ I(lag(oh,1))+I(lag(wv,1))+ I(lag(ky,1)),
                                size.match = 20,
                                qoi = "att",
                                use.diagonal.variance.matrix = T,
                                 lead = 0, forbid.treatment.reversal = F)

DiD.results_chair <- PanelEstimate(confidence.level=c(0.9),sets = DiD.matches_chair, panel.data=panel.data2,df.adjustment = T,number.iterations = 20000)
summary(DiD.results_chair)
plot(DiD.results_chair,main="(3) Main Chair Treatment",ylab="Estimated Average Effect of Treatment on Treated")

cb2<-get_covariate_balance(DiD.matches_chair, panel.data=panel.data2, covariates=c("IncMaj","party2","sen","chairs_other",
                                                                          "nonelses","legislative_seniority","Leader"))

plot(cb2[[1]]$att[,1],type="l",ylim=c(-2,2),ylab="SD",xaxt="n",xlab="Time",main="(4) Main Chair Treatment -\n Covariate Balance",)
lines(cb2[[1]]$att[,2],col="red")
lines(cb2[[1]]$att[,3],col="blue")
lines(cb2[[1]]$att[,4],col="yellow")
lines(cb2[[1]]$att[,5],col="green")
axis(side=1,at=c(1,2),c("t-1","t-0"))
abline(h=0,lty=2)
legend("topleft",col=c("black","red","blue","yellow","green"),lty=1,cex=0.9,legend=c("Party","Senate","Non-Election Cohort","Legislative Seniority","Leader"))
dev.off()

#This estimation involves some simulation, so results might be slightly different than what is 
#in the paper. To reproduce exactly the paper's version of these images, run the following code:
#(please note that commands in current version of PanelMatch, 3.0.0., require different commands to format and present data
#than the version of PanelMatch used for the original analyses, PanelMatch 2.0.0)

rm(list=ls());gc();gc();gc();gc();gc()

load(here("Data", "FigureA11.RData"))
summary(DiD.results_leader)

pdf(file=here("Results", "FigA11Exact.pdf"),width = 9,height = 9)
par(mfcol=c(2,2))

plot(DiD.results_leader,main="(1) Leader Treatment",ylab="Estimated Average Effect of Treatment on Treated")

panel.data1<-PanelData(panel.data=subset(kl_e3,limit1!=1&exc0==0), time.id = "time", unit.id = "cleaned_candid_noskip", treatment = "Leader",outcome="logfriamount")
cb1<-get_covariate_balance(DiD.matches_leader, panel.data=panel.data1, covariates=c("IncMaj","party2","sen","chairs_other",
                                                                                   "nonelses","legislative_seniority","chair_no_other2"))

plot(cb1[[1]]$att[,1],type="l",ylim=c(-2,2),ylab="SD",xaxt="n",xlab="Time",main="(2) Leader Treatment - Covariate Balance",)
lines(cb1[[1]]$att[,2],col="red")
lines(cb1[[1]]$att[,3],col="blue")
lines(cb1[[1]]$att[,4],col="yellow")
lines(cb1[[1]]$att[,5],col="green")
axis(side=1,at=c(1,2),c("t-1","t-0"))
abline(h=0,lty=2)
legend("topleft",col=c("black","red","blue","yellow","green"),lty=1,cex=0.9,legend=c("Majority Party","Party","Senate","Major Chair","Non-Election Cohort","Other Chair"))



# DiD.matches_chair <- PanelMatch(lag = 1, time.id = "time", unit.id = "cleaned_candid", 
#                                 treatment = "chair_no_other2", refinement.method = "CBPS.weight", 
#                                 data = subset(kl_e3,limit1!=1&exc0==0), match.missing = T, verbose=T,
#                                 covs.formula =
#                                   ~ I(lag(Leader,1)) +I(lag(nonelses,1))+I(lag(party2,1))+
#                                   I(lag(IncMaj,1)) +  I(lag(legislative_seniority,1))  + I(lag(sen,1))+
#                                   I(lag(logfriamount,1))+ I(lag(chairs_other,1))
#                                 + I(lag(mi,1))+ I(lag(oh,1))+I(lag(wv,1))+ I(lag(ky,1)),
#                                 size.match = 20,
#                                 qoi = "att",
#                                 use.diagonal.variance.matrix = T,
#                                 outcome.var = "logfriamount", lead = 0, forbid.treatment.reversal = F)
# 
# DiD.results_chair <- PanelEstimate(confidence.level=c(0.9),sets = DiD.matches_chair, data = subset(kl_e3,limit1!=1&exc0==0),df.adjustment = T,number.iterations = 20000)
summary(DiD.results_chair)
plot(DiD.results_chair,main="(3) Main Chair Treatment",ylab="Estimated Average Effect of Treatment on Treated")
panel.data2<-PanelData(panel.data=subset(kl_e3,limit1!=1&exc0==0), time.id = "time", unit.id = "cleaned_candid_noskip", treatment = "chair_no_other2",outcome="logfriamount")
cb2<-get_covariate_balance(DiD.matches_chair, panel.data=panel.data2, covariates=c("IncMaj","party2","sen","chairs_other",
                                                                                   "nonelses","legislative_seniority","Leader"))

plot(cb2[[1]]$att[,1],type="l",ylim=c(-2,2),ylab="SD",xaxt="n",xlab="Time",main="(4) Main Chair Treatment -\n Covariate Balance",)
lines(cb2[[1]]$att[,2],col="red")
lines(cb2[[1]]$att[,3],col="blue")
lines(cb2[[1]]$att[,4],col="yellow")
lines(cb2[[1]]$att[,5],col="green")
axis(side=1,at=c(1,2),c("t-1","t-0"))
abline(h=0,lty=2)
legend("topleft",col=c("black","red","blue","yellow","green"),lty=1,cex=0.9,legend=c("Party","Senate","Non-Election Cohort","Legislative Seniority","Leader"))

dev.off()


